package leaf.science.ui.featuresflayer
{
	import caurina.transitions.Tweener;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	import leaf.science.Global;
	import leaf.science.ui.preloader.Preloader;
	
	public class FeaturesPlayer extends FeaturesPlayerSkin
	{
		private var _screen:Sprite;
		public var isOpen:Boolean;
		
		private var _loader:FeatureLoader;
		private var _preloader:Preloader;
		private var _bodyField:TextField;
		private var _link:MovieClip;
		private var _data:XML;
		
		public function FeaturesPlayer()
		{
			setup();
		}
		
		public function set data (value:XML):void
		{
			_data = value;
			if (isOpen){
				fadeOutText();
				setTimeout(fadeInText, 350);
			}
		}
		
		public function open ():void
		{
			if (isOpen) return;
			
			_preloader.play();
			_preloader.visible = true;
			
			visible = true;
			_screen.visible = true;
			bg_mc.scaleY = 0.02;
			Tweener.addTween(bg_mc, {scaleX:1, time:0.25, transition:'easeOutExpo'});
			Tweener.addTween(bg_mc, {scaleY:1, time:0.2, transition:'easeOutCubic', delay:0.15});
			Tweener.addTween(content, {alpha:1, time:1.4, delay:0.3});
			setTimeout(fadeInText, 800);
			isOpen = true;
		}
		
		private function fadeInText():void
		{
			content.title_txt.text = _data.title.toString();
			_bodyField.htmlText = StringUtils.removeExtraWhitespace(_data.text.toString());
			_bodyField.visible = true;
			
			
			
			switch (_data.title.toString()){
				case 'Lenses':
				_link.gotoAndStop('more');
				_link.visible = true;
				break;
				
				case 'CCD Sensor':
				_link.gotoAndStop('more');
				_link.visible = true;
				break;
				
				case 'Interchangeability':
				_link.gotoAndStop('chart');
				_link.visible = true;
				break;
				
				default:
				_link.visible = false;
			}
			
			
			
			_preloader.play();
			_preloader.visible = true;
			
			_loader.load(new URLRequest(Global.ROOT_PATH + 'image/features/' + _data.image));
			Tweener.addTween(content.grey_mc, {alpha:0, time:1.8});
		}
		
		private function onLoad (evt:Event):void
		{
			
			_preloader.stop();
			_preloader.visible = false;
			
		}
		
		public function close (evt:Event = null):void
		{
			if (!isOpen) return;
			
			_preloader.stop();
			_preloader.visible = false;
			
			fadeOutText();
			setTimeout(doClose, 300);
			isOpen = false;
		}
		
		private function fadeOutText():void
		{
			Tweener.addTween(content.grey_mc, {alpha:1, time:0.3});
			_loader.unload();
		}
		
		private function doClose ():void
		{
			_bodyField.visible = false;
			_link.visible = false;
			_screen.visible = false;
			Tweener.addTween(content, {alpha:0, time:0.2});
			Tweener.addTween(bg_mc, {scaleY:0, time:0.3});
			Tweener.addTween(bg_mc, {scaleX:0, time:0.3, delay:0.2, onComplete:function():void{visible = false; _loader.unload()}});
		}
		
		private function setup():void
		{
			visible = false;
			
			_bodyField = content.body_txt;
			_bodyField.condenseWhite = true;
			_bodyField.visible = false;
			
			bg_mc.scaleX = bg_mc.scaleY = 0;
			content.alpha = 0;
			
			content.grey_mc.mouseEnabled = false;
			
			_link = content.link_mc;
			_link.buttonMode = true;
			_link.visible = false;
			_link.addEventListener(MouseEvent.CLICK, onLink);
			
			_loader = new FeatureLoader();
			_loader.x = content.placeholder_mc.x;
			_loader.y = content.placeholder_mc.y;
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
			content.addChild(_loader);
			
			_preloader = new Preloader(0xFFFFFF);
			_preloader.stop();
			_preloader.visible = false;
			_preloader.x = content.placeholder_mc.width/2;
			_preloader.y = content.placeholder_mc.height/2;
			content.placeholder_mc.addChild(_preloader);
			
			content.cls_mc.stop();
			content.cls_mc.addEventListener(MouseEvent.CLICK, close);
			
			content.next_mc.addEventListener(MouseEvent.CLICK, onNext);
			content.prev_mc.addEventListener(MouseEvent.CLICK, onPrev);
			
			_screen = new Sprite ();
			_screen.graphics.beginFill(0xFFFFFF, 0.5);
			_screen.graphics.drawRect(-1000, -1000, 2000, 2000);
			_screen.addEventListener(MouseEvent.CLICK, onScreenClick);
			addChildAt(_screen, 0);
		}
		
		private function onNext(evt:Event):void
		{
			dispatchEvent(new Event('next'));
		}
		
		private function onPrev(evt:Event):void
		{
			dispatchEvent(new Event('prev'));
		}
		
		private function onLink (evt:MouseEvent):void
		{
			var url:String;
			switch (_data.title.toString()){
				case 'Lenses':
				url = Global.ROOT_PATH + 'pdf/lenses.pdf';
				break;
				
				case 'CCD Sensor':
				url = Global.ROOT_PATH + 'pdf/ccd-sensor.pdf'
				break;
				
				case 'Interchangeability':
				url = Global.ROOT_PATH + 'pdf/Interchangeability.pdf'
				break;
				
				default:
				return;
			}
			navigateToURL(new URLRequest(url), '_blank');
		}
		
		private function onScreenClick (evt:MouseEvent):void
		{
			//
		}
	}
}